nc byuctf.xyz 40014
Files: No files.
Tags: Mediumfrom pwn import *
def wrong_int_to_roman(num):
num_str = str(num)
roman_num = ''
i = 0
while i < len(num_str):
if num_str[i] == '1' and i + 1 < len(num_str) and num_str[i + 1] == '0':
roman_num += 'X'
i += 2
elif num_str[i] == '5':
roman_num += 'V'
i += 1
elif num_str[i] == '1':
roman_num += 'I'
i += 1
else:
i += 1
return roman_num
def wrong_roman_to_int(s):
result_str = ''
for c in s:
if c == 'X':
result_str += '10'
elif c == 'V':
result_str += '5'
elif c == 'I':
result_str += '1'
return int(result_str)
def roman_to_int(s):
roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
result = 0
for i in range(len(s)):
if i > 0 and roman_dict[s[i]] > roman_dict[s[i - 1]]:
result += roman_dict[s[i]] - 2 * roman_dict[s[i - 1]]
else:
result += roman_dict[s[i]]
return result
def int_to_roman(num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i = 0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num
def parse_expression(expr):
# Remove the equals sign and any whitespace from the expression
expr = expr.replace('=', '')
expr = expr.replace(' ', '')
# Find the operator and split the expression into operands
operator = None
operands = []
for op in ['+', '-', '*', '/']:
if op in expr:
operator = op
operands = expr.split(op)
break
# Convert the operands to integers
operands = [roman_to_int(wrong_int_to_roman(operand)) for operand in operands]
print(operands)
# Perform the operation
if operator == '+':
result = operands[0] + operands[1]
elif operator == '-':
result = operands[0] - operands[1]
elif operator == '*':
result = operands[0] * operands[1]
elif operator == '/':
result = operands[0] // operands[1]
# Convert the result back to roman numerals
result = wrong_roman_to_int(int_to_roman(result))
# Return the result
return result
def solve():
p = remote("byuctf.xyz", 40014)
text = p.recvuntilS("=")
problem = text.split("\n")[1]
p.sendline(str(parse_expression(problem)))
print(p.recvallS())
if __name__ == "__main__":
solve()from pwn import *
def wrong_int_to_roman(num):
num_str = str(num)
roman_num = ''
i = 0
while i < len(num_str):
if num_str[i] == '1' and i + 1 < len(num_str) and num_str[i + 1] == '0':
roman_num += 'X'
i += 2
elif num_str[i] == '5':
roman_num += 'V'
i += 1
elif num_str[i] == '1':
roman_num += 'I'
i += 1
else:
i += 1
return roman_num
def wrong_roman_to_int(s):
result_str = ''
for c in s:
if c == 'M':
result_str += '1000'
elif c == 'D':
result_str += '500'
elif c == 'C':
result_str += '100'
elif c == 'L':
result_str += '50'
elif c == 'X':
result_str += '10'
elif c == 'V':
result_str += '5'
elif c == 'I':
result_str += '1'
return int(result_str)
def roman_to_int(s):
roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
result = 0
for i in range(len(s)):
if i > 0 and roman_dict[s[i]] > roman_dict[s[i - 1]]:
result += roman_dict[s[i]] - 2 * roman_dict[s[i - 1]]
else:
result += roman_dict[s[i]]
return result
def int_to_roman(num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i = 0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num
def parse_expression(expr):
# Remove the equals sign and any whitespace from the expression
expr = expr.replace('=', '')
expr = expr.replace(' ', '')
# Find the operator and split the expression into operands
operator = None
operands = []
for op in ['+', '-', '*', '/']:
if op in expr:
operator = op
operands = expr.split(op)
break
# Convert the operands to integers
operands = [roman_to_int(wrong_int_to_roman(operand)) for operand in operands]
# Perform the operation
if operator == '+':
result = operands[0] + operands[1]
elif operator == '-':
result = operands[0] - operands[1]
elif operator == '*':
result = operands[0] * operands[1]
elif operator == '/':
result = operands[0] // operands[1]
result = wrong_roman_to_int(int_to_roman(result))
# Return the result
return result
def solve():
p = remote("byuctf.xyz", 40014)
text = p.recvuntilS("=")
problem = text.split("\n")[1]
p.sendline(str(parse_expression(problem)))
for i in range(999):
if("=" in problem):
problem = p.recvuntilS("=")
p.sendline(str(parse_expression(problem)))
print(f'{i}: {problem}')
else:
p.interactive()
# if("}" in problem):
# print(p.recvallS())
if __name__ == "__main__":
solve()from pwn import *
def wrong_int_to_roman(num):
num_str = str(num)
roman_num = ''
i = 0
while i < len(num_str):
if num_str[i] == '1' and i + 1 < len(num_str) and num_str[i + 1] == '0':
roman_num += 'X'
i += 2
elif num_str[i] == '5':
roman_num += 'V'
i += 1
elif num_str[i] == '1':
roman_num += 'I'
i += 1
else:
i += 1
return roman_num
def wrong_roman_to_int(s):
result_str = ''
for c in s:
if c == 'M':
result_str += '1000'
elif c == 'D':
result_str += '500'
elif c == 'C':
result_str += '100'
elif c == 'L':
result_str += '50'
elif c == 'X':
result_str += '10'
elif c == 'V':
result_str += '5'
elif c == 'I':
result_str += '1'
return int(result_str)
def roman_to_int(s):
roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
result = 0
for i in range(len(s)):
if i > 0 and roman_dict[s[i]] > roman_dict[s[i - 1]]:
result += roman_dict[s[i]] - 2 * roman_dict[s[i - 1]]
else:
result += roman_dict[s[i]]
return result
def int_to_roman(num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i = 0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num
def parse_expression(expr):
# Remove the equals sign and any whitespace from the expression
expr = expr.replace('=', '')
expr = expr.replace(' ', '')
# Find the operator and split the expression into operands
operator = None
operands = []
for op in ['+', '-', '*', '/']:
if op in expr:
operator = op
operands = expr.split(op)
break
# Convert the operands to integers
operands = [roman_to_int(wrong_int_to_roman(operand)) for operand in operands]
# Perform the operation
if operator == '+':
result = operands[0] + operands[1]
elif operator == '-':
result = operands[0] - operands[1]
elif operator == '*':
result = operands[0] * operands[1]
elif operator == '/':
result = operands[0] // operands[1]
result = wrong_roman_to_int(int_to_roman(result))
# Return the result
return result
def solve():
p = remote("byuctf.xyz", 40014)
text = p.recvuntilS("=")
problem = text.split("\n")[1]
p.sendline(str(parse_expression(problem)))
for i in range(499):
if("=" in problem):
problem = p.recvuntilS("=")
p.sendline(str(parse_expression(problem)))
print(f'{i}: {problem}')
p.interactive()
# if("}" in problem):
# print(p.recvallS())
if __name__ == "__main__":
solve()